﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PureMVC.Patterns;
using PureMVC.Interfaces;
using GameEngine.Clients.Silverlight.View;
using GameEngine.Model.Data;
using GameEngine.Clients.Silverlight;
using GameEngine;
using SilverlightClientEditor;
using SilverlightClientEditor.View.Components;
using SilverlightShowCase;
using SilverlightShowCase.View.Components;

namespace MySettlers.Commands
{
    public class Page_EditorEditGame_In_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            SilverlightEditorFacade.Instance.GetType();//Initialize editor facade

            var screen = new GameEditorUC();
            var mediator = MyViewFacade.Helper.CreateMenu<string, GameEditorUC>(
                MyFacade.EditorEditPageMediatorName, 
                screen, 
                MyFacade.PAGE_EDITOREDITGAME_OUT_COMMAND);

            var game = ModelFacade<int>.Instance.Game;
            var lvl = game.CurrentLevel;
            var worlds = lvl.Worlds.GetEnumerator();
            worlds.MoveNext();
            var world = worlds.Current.Value;
            var mapMed = SilverlightEditorViewFacade.Helper.CreateMapPropertiesEditor(world.Map);
            var thingMed = SilverlightEditorViewFacade.Helper.CreateThingEditorMenu(null);
            var worldMed = MyViewFacade.Helper.GetWorldMediator(world.Id);
            var worldEditorMed = SilverlightEditorViewFacade.Helper.CreateWorldPropertiesEditor(world);
            
            screen.setWorldView(worldMed.ViewComponent);
            screen.SetWorldEditorMenu(worldEditorMed.ViewComponent);
            screen.SetThingEditorMenu(thingMed.ViewComponent);
            screen.SetMapEditorMenu(mapMed.ViewComponent);
            //invisible ao tamnho do mapa

            MyFacadeHelper.DesktopNavigate(mediator.ViewComponent);
        }
    }

    public class Page_EditorEditGame_Out_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            var game = ModelFacade<int>.Instance.Game;
            var lvl = game.CurrentLevel;
            var worlds = lvl.Worlds.GetEnumerator();
            worlds.MoveNext();
            var world = worlds.Current.Value;

            switch (notification.Body as string)
            {
                case GameEditorUC.exitButtonTag:
                    MyFacade.Instance.RemoveMediator(MyFacade.EditorEditPageMediatorName);
                    MyFacade.Instance.RemoveMediator(SilverlightEditorViewFacade.Helper.GetThingEditorMenuMediator().MediatorName);
                    MyFacade.Instance.RemoveMediator(SilverlightEditorViewFacade.Helper.GetMapEditorMenuMediator().MediatorName);
                    MyFacade.Instance.RemoveMediator(MyViewFacade.Helper.GetWorldMediator(world.Id).MediatorName);
                    SilverlightApplicationFacadeHelper.DestroyGame();
                    MyFacade.Instance.SendNotification(MyFacade.MY_PAGE_MAIN_IN_COMMAND);
                    break;
                case GameEditorUC.saveButtonTag:
                    MessageBox.Show("TODO");
                    break;
            }
        }
    }
}
